
package com.example.javacrawl.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.javacrawl.mapper.LeftGraphMapper;
import com.example.javacrawl.mapper.RightGraphMapper;
import com.example.javacrawl.pojo.LeftGraphData;
import com.example.javacrawl.pojo.RightGraphData;
import com.example.javacrawl.service.LeftGraphDataService;
import com.example.javacrawl.service.RightGraphDataService;
import com.example.javacrawl.util.AutoGetDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class RightGraphDataServiceImpl extends ServiceImpl<RightGraphMapper, RightGraphData> implements RightGraphDataService {



    @Resource
    private RightGraphMapper rightGraphMapper;
      public void handleData(){
          HashMap<String,Object> dataMap = AutoGetDataUtil.getData();
          if(dataMap == null)return;
            if(dataMap.containsKey("rightGraph")){
                List<RightGraphData> rightGraphDataList = (List<RightGraphData>) dataMap.get("rightGraph");
                for(RightGraphData rightGraphData : rightGraphDataList){
                    String beginYearMonth = rightGraphData.getBeginYearMonth();
                    String endYearMonth = rightGraphData.getEndYearMonth();
                    String begin_end = beginYearMonth +"_"+ endYearMonth;
                    rightGraphData.setBegin_end(begin_end);
                    Integer count = query().eq("buyer_country", rightGraphData.getBuyerCountry()).eq("begin_end",begin_end).count();
                    if(count == 0){
                        // 插入数据

                        System.out.println("保存数据");
                        save(rightGraphData);
                    }else{

                        // 判断是否有数据更新
                        Integer cnt = query().eq("buyer_country", rightGraphData.getBuyerCountry())
                                .eq("begin_end", rightGraphData.getBegin_end())
                                .eq("order_cnt", rightGraphData.getOrderCnt()).count();
                        if(cnt == 1){
                            System.out.println("不用更新了");
                        }else {
                            System.out.println("更新数据");
                            UpdateWrapper<RightGraphData> updateWapper = new UpdateWrapper<>();
                            updateWapper.eq("buyer_country", rightGraphData.getBuyerCountry());
                            updateWapper.eq("begin_end", rightGraphData.getBegin_end());
                            int update = rightGraphMapper.update(rightGraphData, updateWapper);
                            System.out.println(update);
                        }
                    };
                }
            }
        }
}
